package com.lh0811.forge.dependency.basic_code;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DateUtils {

    /**
     * 计算两个日期之间的 相差天数
     *
     * @param startDateStr 格式：yyyy-MM-dd
     * @param endDateStr   格式：yyyy-MM-dd
     * @return
     * @throws Exception
     */
    public static Long countDayNumBetweenDates(String startDateStr, String endDateStr) throws Exception {
        // 使用DateTimeFormatter将字符串解析为LocalDate对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startDateStr, formatter);
        LocalDate endDate = LocalDate.parse(endDateStr, formatter);
        // 计算两个日期之间的天数差
        return ChronoUnit.DAYS.between(startDate, endDate);
    }

    /**
     * 计算两个日期之间的 相差天数
     *
     * @param startDateStr 格式：yyyy-MM-dd
     * @param endDateStr   格式：yyyy-MM-dd
     * @return
     * @throws Exception
     */
    public static List<String> countDayStrBetweenDates(String startDateStr, String endDateStr) throws Exception {
        ArrayList<String> dayStrList = Lists.newArrayList();
        Date day = org.apache.commons.lang3.time.DateUtils.parseDate(startDateStr, "yyyy-MM-dd");
        do {
            dayStrList.add(DateFormatUtils.format(day, "yyyy-MM-dd"));
            day = org.apache.commons.lang3.time.DateUtils.addDays(day, 1);
        } while (DateFormatUtils.format(day, "yyyy-MM-dd").compareTo(endDateStr) <= 0);

        return dayStrList;
    }


    public static void main(String[] args) throws Exception {
        List<String> strings = countDayStrBetweenDates("2024-07-22", "2024-07-22");
        System.out.println(strings);

        String join = StringUtils.join(Lists.newArrayList(), ",");
        System.out.println(join);
    }

}
